﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Xml;

namespace SuperKit
{
    class CommandReader {
        private class Group {
            public string Name { get; }
            public List<Item> Items { get; }

            public Group(string name) {
                Name = name;
                Items = new List<Item>();
            }

            public void Add(Item item) {
                Items.Add(item);
            }
        }


        public class Item {
            public string Name { get; }
            public string Command { get; }
            public string Catalog { get; }
            public string Description { get; }
            public bool IsShortCut { get; }
            public bool IsCommon { get; }

            public Item(string str) {
                string[] arr = str.Split('=');
                Name = arr[0].Trim();
                Command = arr[1].Trim();
            }

            public Item(string name, string command, string catalog, string description, bool isShortCut, bool isCommon) {
                Name = name;
                Command = command;
                Catalog = Kit.IsEmpty(catalog) ? UNKNOWN_CATALOG : catalog;
                Description = description;
                IsShortCut = isShortCut;
                IsCommon = isCommon;
            }

            
        }

        private static CommandReader _instance;
        public static CommandReader Inst => _instance ?? (_instance = new CommandReader());
        public List<Item> Items { get;  }
        private List<Group> groups;

        private const string XML_PATH = "Command.xml";
        private const string DEFAULT_CATALOG = "常用";
        private const string UNKNOWN_CATALOG = "其它";

        public CommandReader() {
            Items = new List<Item>();
            groups = new List<Group>();
            groups.Add(new Group(DEFAULT_CATALOG));
            ReadXml();
        }

        private void ReadXml() {
            XmlDocument xml = new XmlDocument();
            xml.Load(Path.Combine(AppContext.BaseDirectory, XML_PATH));
            XmlNodeList list = xml.SelectNodes("/Root/Item");
            foreach (XmlNode n in list) {
                AddCommand(n);
            }            
        }

        private void AddCommand(XmlNode n) {
            string name = n.Attributes["name"].Value;
            string catalog = n.Attributes["catalog"].Value;
            string command = n["Command"].InnerText;
            string description = n["Description"].InnerText;
            bool shortCut = ReadBoolean(n, "shortcut");
            bool common = ReadBoolean(n, "common");
            
            var item = new Item(name, command, catalog, description, shortCut, common);
            Items.Add(item);
            AddToGroup(item);
        }

        private bool ReadBoolean(XmlNode n, string name) {
            if (n.Attributes[name] != null) {
                return Kit.IsEqual(n.Attributes[name].Value, "true");
            } else {
                return false;
            }
        }

        private void AddToGroup(Item item) {
            foreach (var i in groups) {
                if (item.IsCommon && Kit.IsEqual(i.Name, DEFAULT_CATALOG)) {
                    i.Add(item);
                }

                if (Kit.IsEqual(i.Name, item.Catalog)) {
                    i.Add(item);
                    return;
                }
            }

            Group g = new Group(item.Catalog);
            groups.Add(g);

            g.Add(item);
        }

        public void Fill(TreeView tree) {
            tree.Items.Clear();

            foreach (var g in groups) {
                TreeViewItem item = new TreeViewItem();
                item.Header = g.Name;
                var groupNode = tree.Items.Add(item);
                foreach (var i in g.Items) {
                    TreeViewItem item_command = new TreeViewItem();
                    item_command.Header = i.Name;
                    var node = item.Items.Add(item_command);
                    item_command.DataContext = i;
                }
                item.IsExpanded = true;                
            }

        }

        [Obsolete]
        private void Read() {
            using (StreamReader sr = new StreamReader(Path.Combine(AppContext.BaseDirectory,  "Command.txt"))) {
                string line;
                // 从文件读取并显示行，直到文件的末尾 
                while ((line = sr.ReadLine()) != null) {
                    if (Kit.IsComment(line)) {
                        continue;
                    }
                    Items.Add(new Item(line));
                }
            }
        }

        public string GetCommand(string cmdName) {
            foreach (var i in Items) {
                if (Kit.IsEqual(i.Name, cmdName)) {
                    return i.Command;
                }
            }
            return Kit.EMPTY;
        }
    }
}
